package arena.soldier;

import gameframework.base.MoveStrategy;

import gameframework.game.SpriteManagerDefaultImpl;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

public class Wolf extends SoldierAbstract {
	private static final int FORCE = 2;
	private static final int HEALTHPOINTS = 100;
	public static final int RENDERING_SIZE = 50;
	
	protected boolean movable = true;
	protected boolean active = true;
	protected int enrageTimer = 0;
	
	private final SpriteManagerDefaultImpl spriteManager;
	private MoveStrategy moveStrat;

	public Wolf(String nom, Canvas defaultCanvas) {
		super(nom, HEALTHPOINTS, FORCE);
		spriteManager = new SpriteManagerDefaultImpl("images/wolf.gif",
				defaultCanvas, RENDERING_SIZE, 3);
		spriteManager.setTypes(
				//
				"up",				
				"left",
				"down",
				"right",
				
				"beginAfraid-left",
				"beginAfraid-right",
				"beginAfraid-up",
				"beginAfraid-down" 
				
				);
	}

	public void heal() { //XXX resurrection allowed
		healthPoints = HEALTHPOINTS;
	}
	
	public boolean isActive() {
		return active;
	}

	public void setAlive(boolean aliveState) {
		active = aliveState;
	}
	
	@Override
	public void draw(Graphics g) {
		drawBarLife(g);
		 String spriteType = "";
         Point tmp = getSpeedVector().getDirection();
         movable = true;
         
         if (isEnrage()) {
                 spriteType = "beginAfraid-";
         }

         if (tmp.getX() == -1) {
                 spriteType += "left";
         } else if (tmp.getY() == 1) {
                 spriteType += "down";
         } else if (tmp.getY() == -1) {
                 spriteType += "up";
         } else {
                 spriteType += "right";
         }

         spriteManager.setType(spriteType);
         spriteManager.draw(g, getPosition());
	}

	@Override
	public Rectangle getBoundingBox() {
		return (new Rectangle(0, 0, RENDERING_SIZE, RENDERING_SIZE));
	}

	@Override
	public void oneStepMoveAddedBehavior() {
		if (movable) {
			spriteManager.increment();
		}
	}

	@Override
	public SpriteManagerDefaultImpl getSpriteManager() {
		return spriteManager;
	}

	@Override
	public float getHealthPointAtBeginning() {
		return HEALTHPOINTS;

	}

	@Override
	public float getMagic() {
		return 0;
	}

	@Override
	public float getMagicAtBeginning() {
		return 0;
	}

	public void drawBarLife (Graphics g){
		int lifeBarWidth = getBoundingBox().width/2;
		float life = (getHealthPoints()/getHealthPointAtBeginning());
		g.setColor(Color.BLACK);
		g.drawRect(this.getPosition().x+(getBoundingBox().width)/4, this.getPosition().y, lifeBarWidth, 7);
		
		if (life > 0.33){
			g.setColor(Color.GREEN);
		}
		else
			g.setColor(Color.RED);

		//Drawing life bar inside
		g.fillRect((this.getPosition().x+(getBoundingBox().width)/4)+1,this.getPosition().y+1, (int) ((lifeBarWidth-1)*life), 6);
	}
	
	public void setMoveStrategy(MoveStrategy keyStr) {
		setMoveStrategy(keyStr);
	}

	public MoveStrategy getMoveStrategy() {
		return this.moveStrat;
	}
	
	public boolean isEnrage() {		
		return enrageTimer > 0;
	}

	public void setEnrage(int timer) {
		this.enrageTimer = timer;
	}
}		